var currentTab = null;
var activePage = null;
var maxZ = 1;

window.onload = setTabs;

function setTabs() {
	loadUserInfo();

	var menuTabs = new Array();
	var allElems = document.getElementsByTagName("*");

	for (var i = 0; i < allElems.length; i++) {
		if (allElems[i].className == "tabMenu")
			menuTabs.push(allElems[i]);
	}
	// currentTab = menuTabs[0];
	for (var i = 0; i < menuTabs.length; i++) {
		menuTabs[i].onclick = showTab;
	}
}

function showTab() {
	if (currentTab) {
		currentTab.getElementsByTagName("a")[0].style.color = "#D8D8D8";
		currentTab.style.backgroundColor = "#2E2E2E";
		currentTab.style.borderColor = "#D8D8D8";
	}

	maxZ += 1;
	pageID = this.id + "page";
	activePage = document.getElementById(pageID);
	activePage.style.zIndex = maxZ;
	activePage.style.backgroundColor = "#D8D8D8";
	activePage.style.borderStyle = "solid";
	activePage.style.borderColor = "#2E2E2E";
	activePage.style.borderWidth = "3px 3px 1px 0px";

	currentTab = this;
	this.style.backgroundColor = "#D8D8D8";
	this.style.borderColor = "#2E2E2E";
	this.getElementsByTagName("a")[0].style.color = "#2E2E2E";

	// ajaxPost(getApptList, "../AppointmentServlet", "doctorId='111'");
	getAppts("../AppointmentServlet", data[1]);
}

function getAppts(url, paras) {
	$.get(url, {
		userId : paras,
	}, function(data) {
		var contentDiv = document.getElementById("tabs1page");
		contentDiv.innerHTML = data;
		// set css
		$("#aListId").addClass("tabeAppt");
		// // add event
		// var trs = $("#aListId").find("tr");
		// for (i = 0; i < trs.length; i++) {
		// if (trs[i].id) {
		// //trs[i].onclick = mgrAppt(trs[i].id);
		// //$("#"+trs[i].id).click(mgrAppt(trs[i].id));
		// alert(trs[i].id);
		// document.getElementById(trs[i].id).click=mgrAppt(trs[i].id);
		//				
		// }
		// }

	}, "text");
}

function mgrAppt(apptId) {

	$("#diaDiagnosis").dialog({
		modal : true,
		width : 950,
		height : 720,
		autoOpen : false,
		buttons : {
			"Ok" : function() {
			},
			"Drug List" : function() {
				showDrugDia();
			},
			"Prescribe" : function() {
			},
		},
		show : {
			effect : "blind",
			duration : 1000
		},
		hide : {
			effect : "explode",
			duration : 1000
		}
	});
	$("#diaDiagnosis").dialog("open");
}

function showDrugDia() {
	$("#drugDiv").dialog({
		modal : true,
		width : 800,
		height : 480,
		autoOpen : false,
		buttons : {
			"Ok" : function() {
				$(this).dialog("close");
			},
		}
	});

	$("#drugDiv").dialog("open");

	// ajax get durglist
	// $.get("../QureyDrugsServlet", function(data) {
	// alert("--" + data);
	// }, "text");
	QueryDrugList();
}

function QueryDrugList() {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			txt = "<table border='0' rules=none cellspacing=0 width='100%' style='color:#BE81F7'><tr  align='left' bgcolor='#76C2F8'><th>ID</th><th>Name</th><th>Specification</th></tr>";
			x = xmlhttp.responseXML.getElementsByTagName("RD");
			var length;
			var id;
			var name;
			var spec;
			var i = 0;

			for (i = 0; i < x.length; i++) {
				if (i % 2 == 0)
					txt = txt
							+ "<tr bgcolor='white' onMouseOver='over()' onMouseOut='out()' onclick=''>";
				else
					txt = txt
							+ "<tr bgcolor='#F2F7FB' onMouseOver='over()' onMouseOut='out()' onclick=''>";

				xx = x[i].getElementsByTagName("id");
				{
					try {
						txt = txt + "<td align='center'>"
								+ xx[0].firstChild.nodeValue + "</td>";
					} catch (er) {
						txt = txt + "<td> </td>";
					}
				}
				xx = x[i].getElementsByTagName("name");
				{
					try {
						txt = txt + "<td>" + xx[0].firstChild.nodeValue
								+ "</td>";
					} catch (er) {
						txt = txt + "<td> </td>";
					}
				}
				xx = x[i].getElementsByTagName("specification");
				{
					try {
						txt = txt + "<td>" + xx[0].firstChild.nodeValue
								+ "</td>";
					} catch (er) {
						txt = txt + "<td> </td>";
					}
				}
				txt = txt + "</tr>";
			}

			if (x.length <= 30) {
				for (; i <= 30; i++) {
					if (i % 2 == 0)
						txt = txt + "<tr bgcolor='white'>";
					else
						txt = txt + "<tr bgcolor='#F2F7FB'>";

					txt = txt + "<td>&nbsp;</td>";
					txt = txt + "<td>&nbsp;</td>";
					txt = txt + "<td>&nbsp;</td>";
					txt = txt + "</tr>";
				}
			}

			// console.log("nodeid="+nodeid);
			txt = txt + "</table>";
			document.getElementById("drugDiv").innerHTML = txt;
		}
	};

	xmlhttp.open("POST", "../QureyDrugsServlet", false);
	// xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
	xmlhttp.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	// xmlhttp.setRequestHeader("Content-length", params.length);
	// xmlhttp.setRequestHeader("Connection", "close");

	xmlhttp.send();
}
